'''
Author: your name
Date: 2021-06-29 21:19:54
LastEditTime: 2021-12-09 17:32:24
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \serverd:\project\conan_repo\libevent\conanfile.py
'''

import os
from conans import ConanFile, CMake, tools
from conans import tools
import os
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path


class LibeventConan(ConanFile):
    name = "libevent"
    version = "2.1.2"
    url = "https://gitee.com/mirrors/libevent.git"
    description = "async network library"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"

    def source(self):
        print("source  cwd is", os.getcwd())
        print("sys path is", sys.path)

        my_file = Path("./libevent")
        if my_file.exists():
            print("not need to update source")
        else:
            print("source  cwd is", os.getcwd())
            git = tools.Git(folder="./libevent", verify_ssl=False)
            git.clone(self.url)

    def build(self):
        print("path " + self.package_folder + " is where the package built")
        try:
            os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
        except:
            print("")
        #print("cmake module is " + tools.get_env("CMAKE_MODULE_PATH"))
        print(tools.get_env("BUILD_ENV"))
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="libevent", bashcmd="cmake build -DEVENT__DISABLE_MBEDTLS=ON -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"MinGW Makefiles\" " + tools.unix_path(self.source_folder) + "//libevent")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="libevent", bashcmd="cmake --build .")
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.run("mkdir build",cwd="libevent")
                self.run("cmake  -DEVENT__DISABLE_MBEDTLS=ON -DEVENT__LIBRARY_TYPE:STRING=\"STATIC\"  -G\"Visual Studio 16 2019\" ..",cwd="libevent/build")
                if(self.settings.build_type == "Debug"):
                    self.run("vcvars64.bat && MSBuild ALL_BUILD.vcxproj /t:Rebuild  /m /p:Configuration=Debug", cwd="libevent/build")
                if(self.settings.build_type == "Release"):
                        self.run("vcvars64.bat && MSBuild ALL_BUILD.vcxproj /t:Rebuild  /m /p:Configuration=Release",cwd="libevent/build")
                
        if tools.os_info.is_linux:
            print("current os linux")
            self.run("cmake  -DEVENT__DISABLE_MBEDTLS=ON -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder) + " -G\"Unix Makefiles\" " + tools.unix_path(self.source_folder) + "/libevent",cwd="libevent")
            self.run("mingw32-make  -j 12",cwd="libevent")


    def export(self):
        print("not need to export local file")

    def requirements(self):
        self.requires("OPENSSL/1.1.1")
        self.requires("ZLIB/1.2.11")
        
    def package(self):
        if tools.get_env("BUILD_ENV") == "msys":
            self.copy("libevent/include/*", dst="include", keep_path=True)
            self.copy("libevent/lib/*", dst="lib", keep_path=False)
            self.copy("libevent/lib/*", dst="lib", keep_path=False)

        if tools.get_env("BUILD_ENV") == "msvc64":
            self.copy("libevent/include/*", dst="include", keep_path=True)
            self.copy("libevent/build/lib/Debug/*", dst="lib", keep_path=False)
            self.copy("libevent/build/bin/Debug/*.dll", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.name = 'libevent'
        self.cpp_info.names["cmake_find_package"] = "libevent"
        self.cpp_info.names["cmake_find_package_multi"] = "libevent"

        if self.settings.os == "Windows":
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "libevent"
                self.cpp_info.names["cmake_find_package_multi"] = "libevent"
                self.cpp_info.libdirs = ['libevent/lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libevent_core.a','libevent.a','libevent_openssl.a','libevent_extra.a']
                self.cpp_info.buildirs = 'libevent'
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.cpp_info.names["cmake_find_package"] = "libevent"
                self.cpp_info.names["cmake_find_package_multi"] = "libevent"
                self.cpp_info.libdirs = ['libevent/lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libevent_core.a','libevent.a','libevent_openssl.a','libevent_extra.a',
                   'libevent_core.dll','libevent.dll','libevent_openssl.dll','libevent_extra.dll']
                self.cpp_info.buildirs = 'libevent'

                